﻿using UnityEngine;
using System.Collections;

public class Item : Card {

	public bool isEquipped;
	public int AD;
	public int AP;
	public int Health;
	public int Lifesteal;
	public int numAttacks;
	public Card equippedTo;
	public bool equipMenuOpen;

	// Use this for initialization
	void Start () {

	}

	public void initializeItemStuff(){
		myType = type.item;
	}


	public virtual void equip(Champion champion){
		equippedTo = champion;
		champion.health+=Health;
		champion.attack+=AD;
		champion.abilityPower+=AP;
		champion.lifesteal+=Lifesteal;
		if(GameController.gc.playerOneTurn){
			GameController.gc.playerOneHand.afterPlay(handPositon);
		}
		else{
			GameController.gc.playerTwoHand.afterPlay(handPositon);
		}
		equipMenuOpen = false;
		menuOpen = false;
		transform.position = new Vector3(champion.transform.position.x, champion.transform.position.y, champion.transform.position.z-5);
		StartCoroutine(putUnderChampion(champion));
		removeBackground();
		champion.equippedItems[champion.currentEquippedItemNumber] = this;
		champion.currentEquippedItemNumber++;
		champion.equippedItemsNames += cardName + ", ";
		transform.parent = champion.transform;
		inHand = false;
		GameController.gc.updateLastAction(cardName+" equiped on "+champion.cardName);
		closeMenu();
	}

	public void openEquipMenu(){
		equipMenuOpen = true;
	}

	public virtual float evaluateEquippingItem(Champion champion){
		float hValue = 0;
		hValue += AD * (champion.basicAbilityScalingAd + champion.ultAbilityScalingAd);
		hValue += AP * (champion.basicAbilityScalingAp + champion.ultAbilityScalingAp);
		hValue += Health + champion.maxHealth/10.0f;
		return hValue;
	}

	void OnGUI(){
		if(!GameController.gc.gameover){
			if(equipMenuOpen){
				for(int i = 0; i < 4; i++){
					for(int j = 0; j < 4; j++){
						GridCell targetGridCell = GameController.gc.board[i,j];
						if(targetGridCell.hasCard){
							if(targetGridCell.card.player1Card){
								Vector3 cardPosition = Camera.main.WorldToScreenPoint(targetGridCell.card.transform.position);
								if (GUI.Button(new Rect(cardPosition.x-GameController.gc.cardSize.x/2, Screen.height - GameController.gc.cardSize.y/2 - cardPosition.y, GameController.gc.cardSize.x, GameController.gc.cardSize.y), "")){
									Champion equipTarget = targetGridCell.card;
									equip(equipTarget);
								}
							}
						}
					}
				}
			}
			else if (viewMenuOpen){
				GUI.Box(new Rect(Screen.width * 1/5,Screen.height * 1/10,Screen.width * 3/5,Screen.height * 3/5), GameController.gc.blackBackgroud);
				GUI.DrawTexture(new Rect(Screen.width * 2/10 + 10,Screen.height * 2/10,Screen.width * 3/10,Screen.height * 3/10), cardSprite);
				GUI.Label(new Rect(Screen.width * 5/10,Screen.height * 2/10,Screen.width * 2/10,Screen.height * 1/20),"Item Name: "+ cardName);
				GUI.Label(new Rect(Screen.width * 5/10,Screen.height * 3/10,Screen.width * 2/10,Screen.height * 5/20),"Item Description: "+ cardDescription);
				if(GUI.Button(new Rect(Screen.width * 2/5, Screen.height * 3/5, Screen.width * 1/10, Screen.height * 1/10), "Close"))
					viewMenuOpen = false;
			}
			else if(menuOpen){
				if(GUI.Button(new Rect(Screen.width/2 + 75, Screen.height/2, GameController.gc.buttonSize.x, GameController.gc.buttonSize.y), "Equip"))
					openEquipMenu();
				if(GUI.Button(new Rect(Screen.width/2 - 225, Screen.height/2, GameController.gc.buttonSize.x, GameController.gc.buttonSize.y), "View"))
					view ();
				if(GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2 + 50, GameController.gc.buttonSize.x, GameController.gc.buttonSize.y), "Close"))
					closeMenu();
			}
		}
	}
}
